package com.tech.view.component.button;

import com.tech.view.common.ImageUtil;
import com.tech.view.common.ResourcesUtil;
import com.tech.view.common.SwingTestUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 可设置图标、文字、背景的圆角按钮
 *
 * @deprecated 使用JButton默认的构造器就好，暂时没有这需求
 */
public class IconButton extends JComponent {
	private int width;
	private int height;
	private BufferedImage icon;
	private String text;
	private boolean isHorizontal;
	private int roundSize;
	private Color backgroundColor;

	private JLabel label;
	private JButton button;

	public IconButton(int width, int height, BufferedImage icon, String text, boolean isHorizontal, int roundSize, Color backgroundColor) {
		this.width = width;
		this.height = height;
		this.icon = icon;
		this.text = text;
		this.isHorizontal = isHorizontal;
		this.roundSize = roundSize;
		this.backgroundColor = backgroundColor;

		init();
	}

	public static void main(String[] args) {
		SwingTestUtils.loadSkin();
		// SwingTestUtils.test(new IconButtonOld(200, 200, ResourcesUtil.getImage(ResourcesUtil.Image.LOGO),
		// 		"登录登", true, 0, Color.RED));

		SwingTestUtils.test(new JButton("登录登录", new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.LOGO))));
	}

	private void init() {
		// 大小
		if (width > 0 && height > 0) {
			setPreferredSize(new Dimension(width, height));
		}

		// 文字
		if (text != null) {
			button = new JButton(text);
		}

		// 图标
		if (icon != null) {
			int iconMaxWidth = width - button.getPreferredSize().width;
			int iconMaxHeight = height;
			// 调整图标大小
			if (icon.getHeight() > iconMaxHeight || icon.getWidth() > iconMaxWidth) {
				icon = ImageUtil.createThumbnailFast(icon, iconMaxWidth, iconMaxHeight);
			}

			label = new JLabel(new ImageIcon(icon));

			label.setBounds(0, (height - icon.getHeight()) / 2, icon.getWidth(), icon.getHeight());
			add(label);
		}

		// 按钮
		if (text != null) {
			int iconWidth = icon == null ? 0 : icon.getWidth();
			button.setBounds(iconWidth, 0, width - iconWidth, height);
			add(button);
		}
	}

}
